package com.miracle9.game.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

public class TxLiveUtil {
    public static void main(String[] args) {
        System.out.println(generate_push_url("test","test"));
        System.out.println(general_pull_url("test","test"));
        long timeStamp = System.currentTimeMillis()/1000L + 360000L;
        System.out.println(getSafeUrl("4791af24e90c856089b261271617a815","test",timeStamp));
    }

    private static final char[] DIGITS_LOWER =
            {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};


/*    public static String generate_push_url(String appName,String streamName) {
        String pushUrl = "";
        long expireTime = 360000L;
        String pushDomain = "push.xhqydsh.com.cn";
        String pushKey = "4791af24e90c856089b261271617a815";
        //推流域名未开启鉴权功能的情况下

        long timeStamp = System.currentTimeMillis()/1000L + expireTime;
        pushUrl = "rtmp://"+pushDomain+"/"+appName+"/"+streamName+"?"+getSafeUrl(pushKey,streamName,timeStamp);
        return pushUrl;
    }*/

    public static String generate_push_url(String appName,String streamName) {
        String pushUrl = "";
        long expireTime = 360000L;
        String pushDomain = "push.xhqydsh.com.cn";
   //     String pushKey = "4791af24e90c856089b261271617a815";
        //推流域名未开启鉴权功能的情况下
        long timeStamp = System.currentTimeMillis()/1000L + expireTime;
        pushUrl = "rtmp://"+pushDomain+"/"+appName+"/"+streamName+"";
        return pushUrl;
    }

    public static Map<String,Object> general_pull_url(String appName, String streamName) {
        String rtmpUrl = ""; //rtmp的拉流地址
        String hlsUrl = ""; //m3u8的拉流地址
        String flvUrl = ""; //flv的拉流地址
        String webrtc = ""; //weburl的拉流地址
        Map<String,Object> map = new HashMap<>();
        long expireTime = 360000L;
        String pullDomain = "pull.xhqydsh.com.cn";
        String pullKey = "rETjQGN8DEXzNFPrJCTh";
        //播放域名未配置鉴权Key的情况下

        long timeStamp = System.currentTimeMillis()/1000L + expireTime;
        rtmpUrl = "rtmp://"+pullDomain+"/"+appName+"/"+streamName+"?"+getSafeUrl(pullKey,streamName,timeStamp);


        hlsUrl = "http://"+pullDomain+"/"+appName+"/"+streamName+".m3u8"+"?"+getSafeUrl(pullKey,streamName,timeStamp);

        flvUrl = "http://"+pullDomain+"/"+appName+"/"+streamName+".flv"+"?"+getSafeUrl(pullKey,streamName,timeStamp);
        webrtc = "webrtc://"+pullDomain+"/"+appName+"/"+streamName+"?"+getSafeUrl(pullKey,streamName,timeStamp);

        map.put("rtmpUrl",rtmpUrl);
        map.put("flvUrl",flvUrl);
        map.put("hlsUrl",hlsUrl);
        map.put("webrtc",webrtc);
        return map;
    }
    /*
     * KEY+ streamName + txTime
     */
    private static String getSafeUrl(String key, String streamName, long txTime) {
        String input = new StringBuilder().
                append(key).
                append(streamName).
                append(Long.toHexString(txTime).toUpperCase()).toString();

        String txSecret = null;
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            txSecret  = byteArrayToHexString(
                    messageDigest.digest(input.getBytes("UTF-8")));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return txSecret == null ? "" :
                new StringBuilder().
                        append("txSecret=").
                        append(txSecret).
                        append("&").
                        append("txTime=").
                        append(Long.toHexString(txTime).toUpperCase()).
                        toString();
    }

    private static String byteArrayToHexString(byte[] data) {
        char[] out = new char[data.length << 1];

        for (int i = 0, j = 0; i < data.length; i++) {
            out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS_LOWER[0x0F & data[i]];
        }
        return new String(out);
    }
}
